<!--利率曲线设置-->
<template>
  <div class="app-container flex-cloumn">
    <el-row :gutter="12">
      <el-col :span="4">
        <el-form-item label="利率曲线">
        <baseCombox url="/marketdata/irCurveStrip/queryIrCurveCombox.action"
                    clearable
                    valueField="ircCode"
                    labelField="ircName"
                    :style="{width:'100%'}"
                     placeholder="请选择利率曲线" v-model="queryParams.ircCode"></baseCombox>
      </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item>
        <el-button type="primary" icon="Search" size="small"  v-hasPermi="['curve:ircurve:ircurveset:list']" style="margin: 3px 3px" @click="handleQuery">搜索</el-button>
      </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="利率曲线">
        <baseCombox url="/pricing/comboBox/queryIrcurveFilter.action"
                    clearable
                    :style="{width:'100%'}"
                    @change="handleQuery" placeholder="利率曲线" v-model="queryParams.ircCode"></baseCombox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="small" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form> -->
    <el-row :gutter="12" style="margin: 0px 1px">
        <el-button
            type="primary"
            style="margin: 3px 3px"
            icon="Plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['curve:ircurve:ircurveset:add']"
        >新增</el-button>
        <el-button
            type="success"
            style="margin: 3px 3px"
            icon="Edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['curve:ircurve:ircurveset:upt']"
        >修改</el-button>
        <el-button
            type="danger"
            style="margin: 3px 3px"
            icon="Delete"
            size="mini"
            :disabled="single"
            @click="handleDelete"
            v-hasPermi="['curve:ircurve:ircurveset:del']"
        >删除</el-button>
      <el-button
          type="danger"
          style="margin: 3px 3px"
          icon="Edit"
          size="small"
          :disabled="single || alloFlag"
          @click="handleUpdateAllocate"
          v-hasPermi="['curve:ircurve:allocate']"
      >授权
      </el-button>
<!--        <el-button-->
<!--            type="warning"-->
<!--            style="margin: 3px 3px"-->
<!--            icon="Download"-->
<!--            size="small"-->
<!--            @click="handleExport"-->
<!--            v-hasPermi="['curve:ircurve:ircurveset:export']"-->
<!--        >当页导出</el-button>-->
        <el-button
            type="warning"
            style="margin: 3px 3px"
            icon="Download"
            size="small"
            @click="handleExportAll"
            v-hasPermi="['curve:ircurve:ircurveset:export']"
        >导出</el-button>
    </el-row>

    <xTable  multiple="false" refId="ircurvesetRef" ref="ircurvesetRef" v-loading="loading" :data="irCrvSetList"
             class="tableDragger_form"
             @selection-change="handleSelectionChange">
      <template #columList>
      <el-table-column label="序号" type="index"  width="50">
      </el-table-column>
      <el-table-column label="曲线代码" align="center" prop="ircCode" :show-overflow-tooltip="true" width="200px"/>
      <el-table-column label="曲线名称" align="center" prop="ircName" :show-overflow-tooltip="true" width="200px"/>
      <el-table-column label="币种" align="center" prop="currency" >
        <template #default="scope">
          <dict-tag :options="CURRENCY" :value="scope.row.currency"/>
        </template>
      </el-table-column>

      <el-table-column label="曲线类型" align="center" prop="ircType" >
        <template #default="scope">
          <dict-tag :options="ircType" :value="scope.row.ircType"/>
        </template>
      </el-table-column>

      <el-table-column label="利率类型" :show-overflow-tooltip="true" align="center"  prop="ircRatetype">
        <template #default="scope">
          <dict-tag :options="ircRatetype" :value="scope.row.ircRatetype"/>
        </template>
      </el-table-column>

      <el-table-column label="构建方式" align="center" prop="ircConstructor" >
        <template #default="scope">
          <dict-tag :options="ircConstructor" :value="scope.row.ircConstructor"/>
        </template>
      </el-table-column>

      <el-table-column label="计息基准" align="center" prop="dayCounter" >
        <template #default="scope">
          <dict-tag :options="dayCounter" :value="scope.row.ircDaycount"/>
        </template>
      </el-table-column>

      <el-table-column label="模型方法" align="center" prop="ircMethod" >
        <template #default="scope">
          <dict-tag :options="ircMethod" :value="scope.row.ircMethod"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="模型方法参数" align="center" prop="ircMethodParam" width="100"/>-->
<!--      <el-table-column label="结算日偏移" hidden="true" align="center" prop="settleDays" width="100"/>-->
      <el-table-column label="标的交易代码" align="center" prop="uICode" />
      <el-table-column label="标的资产类型" align="center" prop="uAType" >
        <template #default="scope">
          <dict-tag :options="A_TYPE" :value="scope.row.uAType"/>
        </template>
      </el-table-column>
      <el-table-column label="标的市场类型" align="center" prop="uMType" >
        <template   #default="scope">
          <dict-tag :options="M_TYPE" :value="scope.row.uMType"/>
        </template>
      </el-table-column>
      <el-table-column label="权限" width="100" prop="ircRight">
        <template #default="scope">
          <dict-tag :options="roleType" :value="scope.row.ircRight"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人"  prop="userName">
      </el-table-column>
      </template>
    </xTable>
    <pagination
        style="margin:20px 0 20px 0"
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" v-model="open" width="50%" :close-on-click-modal="false" append-to-body destroy-on-close draggable="true">
      <el-form ref="icvsetRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10">
        <el-col :span="12">
        <el-form-item label="曲线代码" prop="ircCode">
          <el-input v-model="form.ircCode" :disabled="formDisabled.ircCode" clearable
                    size="small"    placeholder="请输入曲线代码"  maxlength="255" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="曲线名称" prop="ircName">
          <el-input v-model="form.ircName" clearable
                    size="small"  placeholder="请输入曲线名称" maxlength="255" />
        </el-form-item>
        </el-col>
<!--          <el-col :span="12" v-if="formShow.ircRight">-->
<!--            <el-form-item label="权限分配" prop="ircRight">-->
<!--              <dictCombox v-model="form.ircRight" filterKey="1,2" dictType="roleType" size="small"></dictCombox>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="24"  v-if="formShow.allocateTree">
            <el-form-item label="授权" prop="ircRight">
              <el-tree
                  class="tree-border"
                  :data="deptUserOptions"
                  show-checkbox
                  :default-expanded-keys="defaultUser"
                  ref="deptUserPermRef"
                  :default-checked-keys="workmates"
                  :default-expand-all="deptUserPermExpend"
                  node-key="id"
                  @check="checkChange"
                  empty-text="加载中，请稍候"
                  :props="{ label: 'label', type: 'type',children:'children' }"
              >
                <template #default="{ node, data }">
                    <span >
                    <span>{{ node.label }}</span>
                      <span v-if="data.type==='u'" >
                        &nbsp;&nbsp;&nbsp;&nbsp;
                      </span>
                    <span v-if="data.type==='u'" >
                      <el-radio-group v-model="treeRadio[data.id.split(':')[1]]" @change="radioChange">
                        <el-radio :label=getReadLabel(data.id)>读权限</el-radio>
                        <el-radio :label=getWriteLabel(data.id)>写权限</el-radio>
                      </el-radio-group>
                    </span>
                  </span>
                 </template>

              </el-tree>
            </el-form-item>
          </el-col>
        <el-col :span="12">
        <el-form-item label="计息基准" prop="ircDaycount">
          <dictCombox  v-model="form.ircDaycount" dictType="dayCounter" size="small"  ></dictCombox>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="币种" prop="currency">
          <dictCombox  v-model="form.currency" dictType="CURRENCY"  size="small"   ></dictCombox>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="曲线类型" prop="ircType">
          <dictCombox  v-model="form.ircType" dictType="ircType" size="small"    ></dictCombox>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="构建方式" prop="ircConstructor">
          <dictCombox  v-model="form.ircConstructor" dictType="ircConstructor"  size="small"   ></dictCombox>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="利率类型" prop="ircRatetype">
          <dictCombox  v-model="form.ircRatetype" dictType="ircRatetype"  size="small"   ></dictCombox>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="标的" prop="uICode">
          <underlyer  ref="underlyer"  v-model="form.uICode" size="small"  > </underlyer>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="模型方法" prop="ircMethod">
          <dictCombox  v-model="form.ircMethod" dictType="ircMethod"  size="small"   ></dictCombox>
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="模型方法参数" prop="ircMethodParam">
          <el-input v-model="form.ircMethodParam" type="textarea" placeholder="请输入模型方法参数" size="small"  maxlength="300"></el-input>
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="参考过滤条件" prop="ircRefGfilter">
          <el-input v-model="form.ircRefGfilter" type="textarea" placeholder="请输入参考过滤条件" size="small" maxlength="300"></el-input>
        </el-form-item>
        </el-col>
      </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!--授权对话框-->
    <el-dialog :title="allocateTitle" v-model="allocateOpen" draggable="true" destroy-on-close
               :close-on-click-modal="false" width="1000px" :height="dialogHeight" append-to-body>
      <el-row :gutter="20">
        <!--部门数据-->
        <el-col :span="4" :xs="24">
          <div class="head-container">
            <el-input
                v-model="deptName"
                placeholder="请输入部门名称"
                clearable
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
                :data="deptOptions"
                :props="{ label: 'label', children: 'children' }"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="deptTreeRef"
                default-expand-all
                @node-click="handleNodeClick"
            />
          </div>
        </el-col>
        <!--用户数据-->
        <el-col :span="20" :xs="24">
          <el-form :model="allocateQueryParams" ref="allocateQueryRef" :inline="true">
            <el-row>
              <el-form-item label="用户编号" prop="userName">
                <el-input
                    v-model="allocateQueryParams.userName"
                    placeholder="请输入用户编号"
                    clearable
                    @keyup.enter="handleAllocateQuery"
                />
              </el-form-item>
              <el-form-item label="用户名称" prop="nickName">
                <el-input
                    v-model="allocateQueryParams.nickName"
                    placeholder="请输入用户名称"
                    clearable
                    @keyup.enter="handleAllocateQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="Search" size="small" @click="handleAllocateQuery">搜索</el-button>
              </el-form-item>
            </el-row>
          </el-form>
          <el-row :gutter="10" class="mb8" style="margin-top:5px">
            <el-col :span="1.5">
              <el-button
                  type="success"
                  icon="Edit"
                  size="small"
                  :disabled="allocateMultiple || adminShow"
                  v-show="rows[0].ircRight >= 2"
                  @click="handleWriteDefault"
              >写权限
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                  type="success"
                  icon="Edit"
                  size="small"
                  v-show="rows[0].ircRight >= 1"
                  :disabled="allocateMultiple || adminShow"
                  @click="handleReadDefault"
              >读权限
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                  type="success"
                  icon="Edit"
                  size="small"
                  :disabled="allocateMultiple || adminShow"
                  @click="handleNoneDefault"
              >取消权限
              </el-button>
            </el-col>

          </el-row>
          <xTable ref="allocateUserTable" refId="allocateUserTable"
                  v-loading="allocateLoading" :data="allocateUserList"
                  :style="{height:screenHeight , overflow:'auto'}"
                  @selection-change="allocateUserHandleSelectionChange">
            <template #columList>
              <el-table-column label="用户编号" align="center" prop="userName" :show-overflow-tooltip="true"/>
              <el-table-column label="用户名称" align="center" prop="nickName" :show-overflow-tooltip="true"/>
              <el-table-column label="所属部门" align="center" prop="deptName" :show-overflow-tooltip="true"/>
              <el-table-column label="曲线代码" align="center" prop="ircCode" :show-overflow-tooltip="true"/>
              <el-table-column label="读写权限" align="center" prop="ircRight" class-name="small-padding fixed-width">
                <template #default="scope">
                  <dict-tag :options="roleType" :value="scope.row.status"/>
                </template>
              </el-table-column>
            </template>
          </xTable>
          <pagination
              v-show="allocateUserTotal > 0"
              :total="allocateUserTotal"
              v-model:page="allocateQueryParams.pageNum"
              v-model:limit="allocateQueryParams.pageSize"
              @pagination="getAllocateUser"
          />
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup name="Ircurveset">
import {getIrCrvSetList,addIrcCurveSet,uptIrcCurveSet,delIrcCurveSet,listAllocateUser,updateUserAuthority} from "@/api/curve/ircurve/ircurveset";
import {ref} from "vue";
import store from '@/store'
import {treeselectuser, treeselect} from "@/api/system/dept";
import {isNotVoid} from "../../../../utils/util";

const { proxy } = getCurrentInstance();
const { M_TYPE,A_TYPE,CURRENCY,ircType,ircConstructor,ircMethod,dayCounter,ircRatetype,roleType} = proxy.useDict("M_TYPE","A_TYPE","CURRENCY","ircType","ircConstructor","ircMethod","dayCounter","ircRatetype","roleType");

const irCrvSetList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const rows = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const action= ref('');

const deptUserOptions = ref([]);
const deptUserPermRef = ref(null);
const alloFlag = ref(false)
const deptUserPermExpend = ref(false);
const defaultUser = ref(['u:' + store.getters.user.userId])
//授权对话框
const deptId = ref();
const workmates = ref([]);
const allocateUserList = ref([]);
const allocateTitle = ref("");
const allocateOpen = ref(false);
const allocateLoading = ref(true);
const allocateUserTotal = ref(0);
const deptOptions = ref(undefined);
const deptName = ref("");
const userIds = ref([]);
const adminShow = ref(false);
const allocateSingle = ref(true);
const allocateMultiple = ref(true);

const screenHeight = ref(document.documentElement.clientHeight - 350 + "px");
const dialogHeight = ref(document.documentElement.clientHeight - 200 + "px");

const data = reactive({
  form:{},
  treeRadio:[],
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    ircCode: undefined
  },
  rules: {
    ircCode:[{ required: true, message: "曲线代码不能为空", trigger: "blur" }],
    ircName:[{ required: true, message: "曲线名称不能为空", trigger: "blur" }],
    ircDaycount:[{ required: true, message: "计息基准不能为空", trigger: "blur" }],
    currency:[{ required: true, message: "币种不能为空", trigger: "blur" }],
    ircType:[{ required: true, message: "曲线类型不能为空", trigger: "blur" }],
    ircConstructor:[{ required: true, message: "构建方式不能为空", trigger: "blur" }],
    ircRatetype:[{ required: true, message: "利率类型不能为空", trigger: "blur" }],
    uICode:[{ required: false, message: "标的不能为空", trigger: "blur" }],
    ircMethod:[{ required: true, message: "模型方法不能为空", trigger: "blur" }],
    ircRight:[{ required: true, message: "权限分配不能为空", trigger: "blur" }],
    allocateTree:[{ required: true, message: "授权不能为空", trigger: "blur" }]
  },
  formDisabled: {
    ircCode: false,
  },
  formShow: {
    allocateTree : false,
    ircRight: false,
  },
  allocateQueryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: undefined,
    deptName: undefined,
    userName: undefined,
    nickName: undefined,
    ircCode: undefined
  },
});

const { queryParams,form,rules ,formDisabled,formShow, allocateQueryParams,treeRadio} = toRefs(data);
/** 表单重置 */
function reset() {
  form.value = {
    ircCode:undefined,
    ircName:undefined,
    ircDaycount:'Actual/360',
    currency:undefined,
    ircType:undefined,
    ircConstructor:undefined,
    ircRatetype:undefined,
    uICode:undefined,
    ircMethod:undefined,
    ircMethodParam:undefined,
    ircRefGfilter:undefined,
    ircRight: '1',
  };
  proxy.resetForm("icvsetRef");
}
/** 查询列表 */
function getList() {
  loading.value = true;
  getIrCrvSetList(queryParams.value).then(response => {
    irCrvSetList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("/marketdata/irCurveStrip/export", {
    ...queryParams.value
  }, `利率曲线定义${new Date().getTime()}.xlsx`);
}

/** 导出按钮操作 */
function handleExportAll() {
  proxy.download("/marketdata/irCurveStrip/export", Object.assign({
    ...queryParams.value
  },{pageSize:''}), `利率曲线定义${new Date().getTime()}.xlsx`);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 通过条件过滤节点  */
const filterNode = (value, data) => {
  if (!value) return true;
  return data.label.indexOf(value) !== -1;
};
/** 根据名称筛选部门树 */
watch(deptName, val => {
  proxy.$refs["deptTreeRef"].filter(val);
});

/** 查询部门下拉树结构 */
function getTreeselect() {
  treeselect().then(response => {
    deptOptions.value = response.data;
    handleNodeClick({id: store.getters.user.deptId});
  });
};

/** 节点单击事件 */
function handleNodeClick(data) {
  allocateQueryParams.value.deptId = data.id;
  handleAllocateQuery();
};

//点击授权里的搜索按钮
function handleAllocateQuery() {
  allocateQueryParams.value.pageNum = 1;
  getAllocateUser();
}


function allocateUserHandleSelectionChange(selection) {
  userIds.value = selection.map(item => item.userId);
  if (selection.length != 0) {
    deptId.value = selection[0].deptId;
  }
  if (userIds.value.includes(store.getters.user.userId) === true||userIds.value.includes(1) === true) {
    adminShow.value = true
  } else {
    adminShow.value = false
  }
  allocateSingle.value = selection.length != 1;
  allocateMultiple.value = !selection.length;
}

/** 授权按钮操作 */
function handleUpdateAllocate() {
  allocateOpen.value = true;
  allocateTitle.value = "授权";
  allocateQueryParams.value.ircCode = rows.value[0].ircCode;
  getTreeselect();
}

function getAllocateUser() {
  allocateLoading.value = true;
  listAllocateUser(allocateQueryParams.value).then(response => {
    allocateUserList.value = response.rows;
    allocateUserList.value.forEach(element => {
      element.ircCode = rows.value[0].ircCode;
    })
    allocateUserTotal.value = response.total;
    allocateLoading.value = false;
  });
}

/** 写权限 */
function handleWriteDefault() {
  handleAuth('2');
}

/** 读权限 */
function handleReadDefault() {
  handleAuth('1');
}

/** 无权限 */
function handleNoneDefault() {
  handleAuth('0');
}

/** 分配权限操作 */
function handleAuth(ircRight) {
  var params = {};
  params.ircCode = rows.value[0].ircCode;
  params.deptId = deptId.value;
  params.userIds = userIds.value;
  params.ircRight = ircRight;

  updateUserAuthority(params).then(response => {
    if (response.success) {
      proxy.$modal.msgSuccess("设置成功");
      getAllocateUser();
    } else {
      proxy.$modal.msgError(response.msg);
    }
  });

}

// ============权限分配
/** 查询部门用户下拉树结构 */
function getTreeselectuser() {
  treeselectuser().then(response => {
    deptUserOptions.value = response.data;
  });
};



getTreeselectuser();


/** 新增按钮操作 */
function handleAdd() {
  reset();
  deptUserPermExpend.value=false;
  defaultUser.value = ['u:' + store.getters.user.userId];
  let tempWorkMates = [];
  let a = proxy.treeFindPath(deptUserOptions.value,node => {
    if(node.id == defaultUser.value){
      return true;
    }
  });
  treeRadio.value=[];
  a[a.length -2].children.forEach(item => {
    if(item.id.search("u:") != -1){
      tempWorkMates.push(item.id);
      let uId=item.id.split(':')[1];
      if(uId==store.getters.user.userId){
        treeRadio.value[uId]=uId+'-2';
      }else{
        treeRadio.value[uId]=uId+'-1';
      }
    }
  })
  workmates.value = tempWorkMates;
  action.value = 'add';
  formShow.value.ircRight = true;
  formShow.value.allocateTree = true;
  formDisabled.value.ircCode=false;
  open.value = true;
  title.value = "添加曲线定义";
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  if (selection.length === 1 && (selection[0].ircRight == '2' || store.getters.user.userId == '1')) {
    alloFlag.value = false;
  } else {
    alloFlag.value = true;
  }
  if (selection.length == 0) {
    ids.value = [];
  } else {
    ids.value = selection.map(item => item.ircCode);
    rows.value = selection.map(item => item);
  }
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  if (rows.value[0].ircRight === '1') {
    proxy.$modal.alertError("没有权限修改该数据");
    return;
  }
  reset();
  formShow.value.ircRight = false;
  formShow.value.allocateTree = false;
  let ircCode = row.ircCode || ids.value;
  if(Array.isArray(ircCode)){
    ircCode=ircCode[0];
  }
  action.value = 'update';
  open.value = true;
  formDisabled.value.ircCode=true;
  getIrCrvSetList({'ircCode':ircCode}).then(response => {
    form.value = response.rows[0];
    if(response.rows[0]!=null&response.rows[0].uICode!=""&&response.rows[0].uICode!=null){
      form.value.uICode=response.rows[0].uICode+','+response.rows[0].uAType+','+response.rows[0].uMType;
     }

    title.value = "修改曲线定义";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["icvsetRef"].validate(valid => {
    if (valid) {
      if(form.value.uICode!=null&&form.value.uICode!=''){
        let underIds=form.value.uICode.split(',');
        form.value.uiCode=underIds[0];
        form.value.uaType=underIds[1];
        form.value.umType=underIds[2];
      }
      if (action.value=='update') {
        uptIrcCurveSet(form.value).then(response => {
          if(response.success){
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          }else{
            proxy.$modal.msgWarning(response.msg);
          }

        });
      } else if(action.value == 'add'){
        getAllCheckedDeptAndUsers();
        let addParam = proxy.lsh.cloneDeep(form.value);
        let radioArray={};
        proxy.lsh.forEach(treeRadio.value,function(val,ky){
          if(val!=undefined&&val!=null&&isNotVoid(val.split("-")[1])){
            radioArray[val.split("-")[0]]=val.split("-")[1];
          }
        })

        proxy.lsh.assign(addParam, {'ircRight':JSON.stringify(radioArray)});
        addIrcCurveSet(addParam).then(response => {
          if(response.success){
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          }else {
            proxy.$modal.msgWarning(response.msg);
          }
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  if (rows.value[0].ircRight === '1') {
    proxy.$modal.alertError("没有权限删除该数据");
    return;
  }
  let ircCode = row.ircCode || ids.value;
  if(Array.isArray(ircCode)){
    ircCode=ircCode[0];
  }
  proxy.$modal.confirm('是否确认删除利率曲线编号为"' + ircCode + '"的数据项？').then(function() {
    return delIrcCurveSet({'ircCode':ircCode});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


/**获取当前选中的所有节点 */
function getAllCheckedDeptAndUsers() {
  let checkedNodes = deptUserPermRef.value.getCheckedNodes();
  let halfCheckedNodes = deptUserPermRef.value.getHalfCheckedNodes();
  let valueIdArr = [];
  checkedNodes.forEach(element => {
    valueIdArr.push(element.id);
  })
  halfCheckedNodes.forEach(element => {
    valueIdArr.push(element.id);
  })
  form.value.allocateDeptAndUser = valueIdArr.join(",")
}

function checkChange(data, option) {
  let checkStatus=false;
  option.checkedKeys.forEach(function(option,index) {
    if(option==data.id){
      checkStatus=true;
    }
  });
  if (data.id === defaultUser.value[0]) {
    deptUserPermRef.value.setChecked(data, true, true);
    treeRadio.value[data.id.split(":")[1]]=data.id.split(":")[1]+'-2';
  }else{
    let uId=data.id.split(":")[1];
    if(checkStatus){
      if(treeRadio.value[uId]!=undefined&&treeRadio.value[uId].split('-')[1]=='2') {
        treeRadio.value[uId] = uId + '-2';
      }else{
        treeRadio.value[uId] = uId + '-1';
      }
      //如果为其他节点时处理子节点 读写
      let nodeArray=[];
      putChildOfTree(nodeArray,data);
      proxy.lsh.forEach(nodeArray, function (opt) {
        if(opt.id.split(':')[0]=='u'){
          let uId2=opt.id.split(':')[1];
          if((treeRadio.value[uId2]!=undefined&&treeRadio.value[uId2].split('-')[1]=='2')||uId2==defaultUser.value[0]) {
            treeRadio.value[uId2] = uId2 + '-2';
          }else{
            treeRadio.value[uId2] = uId2 + '-1';
          }
        }
      });
    }else{
      treeRadio.value[uId]=uId+'-0';
      //如果为其他节点时处理子节点 读写
      let nodeArray=[];
      putChildOfTree(nodeArray,data);
      proxy.lsh.forEach(nodeArray, function (opt) {
        if(opt.id.split(':')[0]=='u'){
          let uId2=opt.id.split(':')[1];
          if(uId2===defaultUser.value[0].split(":")[1]) {
            deptUserPermRef.value.setChecked(defaultUser.value[0], true, true);
            treeRadio.value[uId2] = uId2 + '-2';
          }else{
            treeRadio.value[uId2] = uId2 + '-0';
          }
        }
      });
    }

  }
}
function getReadLabel(key){
  let uId=key.split(":")[1];
  return uId+'-1';
}
function getWriteLabel(key){
  let uId=key.split(":")[1];
  return uId+'-2';
}
function radioChange(value){
  let uId=value.split("-")[0];
  if(uId==store.getters.user.userId){
    treeRadio.value[uId]=uId+'-2';
  }
  deptUserPermRef.value.setChecked({id:'u:'+uId}, true, true);
}

function putChildOfTree(nodeArray,data){
  if(data.children!=undefined&&data.children!=null&&data.children.length>0) {
    proxy.lsh.forEach(data.children, function (childData) {
      nodeArray.push(childData);
      putChildOfTree(nodeArray,childData);
    });
  }
}
getList();
</script>
